package com.studyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.studyadmin.entity.ChapterContent;
import com.studyadmin.entity.StudentVideo;
import com.studyadmin.entity.Video;
import com.studyadmin.mapper.StudentVideoMapper;
import com.studyadmin.mapper.VideoMapper;
import com.studyadmin.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyadmin.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 章节视频内容表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-25
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired(required = false)
    VideoMapper videoMapper;
    @Autowired(required = false)
    StudentVideoMapper studentVideoMapper;


    @Override
    public JsonUtil chapterVideoContentSer(String chaId) {

        JsonUtil jsonUtil = new JsonUtil();

        Video chapters=videoMapper.selectOne(
                new QueryWrapper<Video>()
                        .eq("cha_id",Integer.parseInt(chaId))
        );
        if(chapters==null){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("查询成功");
            jsonUtil.setData(chapters);
        }
        return jsonUtil;
    }

    /**
     * 请求后端数据：学生id，视频id，章节id，视频总时长
     *
     * 插入数据库student_video 要求：该记录已经存在 则更新学习视频时长
     * 依据视频 id learningTime进度条
     * @param studentVideo 学生id，视频id，章节id，视频总时长
     * @return 插入情况
     */
    @Override
    public JsonUtil chapterStudentVideoSer(StudentVideo studentVideo) {

        JsonUtil jsonUtil = new JsonUtil();
        String videoId = studentVideo.getVideoId();
        System.out.println("找到登录人信息没有？"+studentVideo);
        StudentVideo studentVideo1=studentVideoMapper.selectOne(
                new QueryWrapper<StudentVideo>()
                        .eq("stu_num",studentVideo.getStuNum())
                        .eq("video_id",videoId)
        );


        if(studentVideo1 == null){
            System.out.println("------------");
            studentVideo.setCreateTime(LocalDateTime.now());
            studentVideo.setUpdateTime(LocalDateTime.now());
            studentVideo.setId(String.valueOf(System.currentTimeMillis()));

            //learning_time, duration,学习时间和视频总时长比较
            int accomplish_flag = 0;
            if(studentVideo.getDuration()-3 < studentVideo.getLearningTime()){
                accomplish_flag=1;
            }

            float lastTime= studentVideo.getLearningTime();
            studentVideo.setLastTime(lastTime);
            studentVideo.setAccomplishFlag(accomplish_flag);
            studentVideo.setDelFlag(0);
            int result=studentVideoMapper.insert(studentVideo);
            if(result<0){
                jsonUtil.setCode(1);
                jsonUtil.setMsg("插入失败");
            }else {
                jsonUtil.setCode(0);
                jsonUtil.setMsg("插入成功");
            }

        }else {
            if(studentVideo1.getAccomplishFlag()==1){
                jsonUtil.setCount(3);
                jsonUtil.setMsg("学习完成");
            }
            //更新数据
            StudentVideo studentVideo2 = new StudentVideo();
//            System.out.println(studentVideo1);

            studentVideo2.setId(studentVideo1.getId());
            studentVideo2.setCreateTime(LocalDateTime.now());
            studentVideo2.setLearningTime(studentVideo1.getLearningTime()+studentVideo.getLearningTime());
            studentVideo2.setLastTime(studentVideo.getLearningTime());
            //总视频的前5s视为完成视频
            int accomplish_flag = 0;
            if(studentVideo1.getDuration()-3 < studentVideo.getLearningTime()){
                accomplish_flag=1;
            }
            studentVideo2.setAccomplishFlag(accomplish_flag);

            int res=studentVideoMapper.updateById(studentVideo2);
            if(res<0){
                jsonUtil.setCode(1);
                jsonUtil.setMsg("更新失败");
            }else {
                jsonUtil.setCode(0);
                jsonUtil.setMsg("更新成功");
            }
        }
        return jsonUtil;
    }

    /**
     * 插入或更新视频
     * @param video
     * @return
     */
    @Override
    public JsonUtil updateVideoUrlSer(Video video) {
        JsonUtil jsonUtil = new JsonUtil();
       //如果是修改，先要进行判断是否存在该视频，再进行逻辑处理
        //暂时省略
        video.setCreateTime(LocalDateTime.now());
        video.setUpdateTime(LocalDateTime.now());
        video.setDelFlag(0);
        String s= StringUtils.substringAfter(video.getVideoSourceUrl(), "examples/");
        video.setVideoOriginalName(s);
        System.out.println("插入的数据："+video);
        int res=videoMapper.insert(video);
        if(res<0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("修改失败");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("修改成功");
        }


        return jsonUtil;
    }
}
